﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CpdatCloudClient.Interop;

namespace Excel2Word
{
    public class Dics
    {


        public enum colDataType
        {
            数字, 文本, 日期, 长文本
        }

        public enum FixColName
        {
            GUID, ROW_NUMBER_, UPDATE_TIME_, UPDATE_ID_
        }


        public const string connStr = "srfa";
        public const string dbTableModel = "ttsurvay_model";
        public const string dbTableTask = "t_form";
        public const string dbTableProject = "t_project";

        public const string groupProvinceManager = "铁塔院省级负责人";
        public const string groupCityManager = "铁塔院地市负责人";
        public const string groupLeader = "铁塔院部门领导";
        public const string groupAdmin = "铁塔院系统管理员";

        public const string uploadPathForExcel = "FormExcelReport";
        public const string uploadPathForReview = "FormReportForReview";
        public const string uploadPathForWord = "FormReport";
        public const string uploadPathForSiteTable = "SiteTable";

        public const string WORD_FIELD_TAG = "ttSurvey_";



        public const string word_file_image_group = "image_group";
        public const string word_file_image_image = "image_image";
        public const string word_file_not_image = "not_image";
        public const string excel_file_image_group_v = "image_grp_v";
        public const string excel_file_image_group_h = "image_grp_h";
        public const string excel_file_image_image = "image_image";
        public const string excel_file_field = "field_field";
        public const string excel_file_field_pc = "field_input";

        //https://58.240.86.44:23589/srfa/cadtemplate/upload
        //http://127.0.0.1:10000/cadtemplate/upload
        //https://58.240.86.44:23589/srfa/
        //http://127.0.0.1:10000/
        public const string java_server_address = "https://58.240.86.44:23589/srfa/";

        /// <summary>
        /// 获取省级负责人的省列表
        /// </summary>
        public static List<string> myProvinceList;
        public static List<string> GetProvinceList()
        {
            if (myProvinceList == null && GetPriorityLevel() == 3)
            {
                var groupInfo = GetGroupInfo().Find(the => the.groupName.Equals(groupProvinceManager));
                if (groupInfo != null && groupInfo.groupDesc.Length > 0)
                {
                    return groupInfo.groupDesc.Replace("，", ",").Split(',').ToList();
                }

            }
            return myProvinceList;
        }

        /// <summary>
        /// 获取地市负责人的地市列表
        /// </summary>
        public static List<string> myCityList;
        public static List<string> GetCityList()
        {
            if (myCityList == null && GetPriorityLevel() == 4)
            {
                var groupInfo = GetGroupInfo().Find(the => the.groupName.Equals(groupCityManager));
                if (groupInfo != null && groupInfo.groupDesc.Length > 0)
                {
                    return groupInfo.groupDesc.Replace("，", ",").Split(',').ToList();
                }

            }
            return myCityList;
        }


        /// <summary>
        /// 获取权限级别
        /// </summary>
        public static int priorityLevel = 999; //权限等级 1级最高为系统管理员，2级为部门领导，3级为省级负责人，4级为地市负责人，5级为普通员工；
        public static int GetPriorityLevel()
        {
            if (priorityLevel == 999)
            {
                priorityLevel = 5;
                var gList = GetGroupInfo();
                if (gList != null)
                {
                    if (gList.Exists(the => the.groupName.Equals(groupCityManager))) priorityLevel = 4;
                    if (gList.Exists(the => the.groupName.Equals(groupProvinceManager))) priorityLevel = 3;
                    if (gList.Exists(the => the.groupName.Equals(groupLeader))) priorityLevel = 2;
                    if (gList.Exists(the => the.groupName.Equals(groupAdmin))) priorityLevel = 1;
                }
            }
            return priorityLevel;
        }



        public static bool isWx()
        {
            var gList = GetGroupInfo();
            if (gList != null)
            {
                if (gList.Exists(the => the.groupName.Equals("铁塔院外协"))) return true;
            }
            return false;
        }


        public static CpdatCloudClient.Interop.PlugInterop cpdatApi;

        public static CpdatCloudClient.Interop.PlugInterop GetDbClient()
        {
            if (cpdatApi == null) cpdatApi = new CpdatCloudClient.Interop.PlugInterop();
            
            return cpdatApi;
        }



        public static CpdatCloudClient.Interop.PlugInterop GetCpdatApi()
        {
            if (cpdatApi == null) cpdatApi = new CpdatCloudClient.Interop.PlugInterop();
            return cpdatApi;
        }

        /// <summary>
        /// 获取用户在铁塔查勘系统注册的部门
        /// </summary>
        public static string userDepartment;
        public static string GetUserDepartment()
        {
            if (userDepartment == null)
            {
                var dt = Dics.GetDbClient().GetDataFromDb(Dics.connStr, string.Format("SELECT department FROM t_user where userId = '{0}'", GetUserInfo().userId)).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    var tempDepart = dt.Rows[0]["department"];
                    var dept = tempDepart == null ? "" : tempDepart.ToString();
                    return dept;
                }
                return null;
            }
            else
            {
                return userDepartment;
            }

        }


        /// <summary>
        /// 获取用户的登录信息
        /// </summary>
        public static UserInfo userInfo;
        public static UserInfo GetUserInfo()
        {
            if (userInfo == null)
            {
                try
                {
                    userInfo = JsonConvert.DeserializeObject<UserInfo>(GetDbClient().GetUserInfo(true));
                    if (String.IsNullOrEmpty(userInfo.deptId1))
                    {
                        System.Windows.Forms.MessageBox.Show("您的账号缺失部门信息，请联系管理员添加, 部门信息不完整会影响使用");
                    }
                    //userInfo.userId = "gwc";
                }
                catch (Exception)
                {
                    System.Windows.Forms.MessageBox.Show("\r\n请登录CPDAT云终端！");
                    Environment.Exit(0);
                }

            }
            return userInfo;
        }

        /// <summary>
        /// 获取用户的角色信息
        /// </summary>
        public static List<GroupInfo> groupInfo;
        public static List<GroupInfo> GetGroupInfo()
        {
            if (groupInfo == null)
            {
                try
                {
                    groupInfo = GetDbClient().GetGroupList(GetUserInfo().userId);
                }
                catch (Exception)
                {
                    System.Windows.Forms.MessageBox.Show("\r\n请登录CPDAT云终端！");
                    throw;
                }

            }
            return groupInfo;
        }


        /// <summary>
        /// 获取省列表
        /// </summary>
        public static Dictionary<string, List<string>> provinceDic;
        public static Dictionary<string, List<string>> GetProvinceDic()
        {
            if (provinceDic == null)
            {
                try
                {
                    provinceDic = new Dictionary<string, List<string>>();
                    string sqlStr = "SELECT * FROM DIC_CITY";
                    var dt = GetDbClient().GetDataFromDb(Dics.connStr, sqlStr).Tables[0];
                    List<IGrouping<string, DataRow>> gList = dt.AsEnumerable().GroupBy(x => x.Field<string>("PROVINCE")).ToList();
                    foreach (IGrouping<string, DataRow> g in gList)
                    {
                        var tempList = new List<string>();
                        foreach (DataRow dr in g)
                        {
                            tempList.Add(dr["CITY_NAME"].ToString());
                        }
                        provinceDic[g.Key] = tempList;
                    }
                    return provinceDic;
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(String.Format("{0}\r\n你可能未登录CPDAT云终端", ex.Message));
                    throw;
                }
            }
            return provinceDic;
        }
    }
}
